#include "systemdatabase.h"
#include <QMessageBox>

SystemDataBase::SystemDataBase()
{
    SystemDataBase::connect();
}

bool SystemDataBase::connect(){
    QSqlDatabase db;


    if(QSqlDatabase::contains("qt_sql_default_connection")){
        db = QSqlDatabase::addDatabase("qt_sql_default_connection");
    }
    else{
    /*
        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("OFMS.db");
        db.setUserName("root");
        db.setPassword("123456");
        */
        db = QSqlDatabase::addDatabase("QODBC");//a
        db.setHostName("127.0.0.1");             //b
        db.setDatabaseName("test04");              //c
        db.setUserName("root");                  //d
        db.setPassword("123456");
    }

    if (db.open()) {
        QMessageBox::information(NULL,QObject::tr("数据库连接成功"),
                                 QObject::tr("已经连接上数据库文件OFMS.db"));
        return true;
    }
    else{
        QMessageBox::critical(0, QObject::tr("数据库连接失败"),
                              db.lastError().text());
        return false;
    }
}
